<?php

Route::post('write', function(){
	//导出模型文件
	$modelId = Input::get('modelId');
	$model   = Model::find($modelId);
	$fields  = Field::where('model_id', $modelId)->get();
	$str     = '<meta content="text/html; charset=utf8" />'."\n";
	$str     = $str.'<model="'.$model['table_name'].'" />'."\n";
	foreach ($fields as $key => $field) {
		$str = $str.'<field="'.$field['field_name'].'" displayName="'.$field['display_name'].'" filedType="'.$field['field_type'].'" />'."\n";
	}
	return File::put("models/".$model['table_name'].".html", $str, true);
});

Route::get('read', function(){
	// 导入字段
	$tagArray  = file('models/article.html');
	foreach ($tagArray as $key => $tag) {
		//存入modelName
		if(str_contains($tag, '<model="')){
			$tagName = explode(' ', $tag);
			foreach ($tagName as $key => $value) {
				if(stristr($value, '"')){
					if(strlen(strrchr($value, '"')) == 1){
						$modelName = substr(stristr($value, '"'), 1, strlen(stristr($value, '"'))-2);
						// $model     = new Model;
						// $model->model_name = $modelName;
						// $model->table_name = $tableName;
						// $model->save();
						// $modelId   = $model->id;
					}
				}
			}
		}

		if(str_contains($tag, '<field="')){
			$field = explode(' ',$tag);
			foreach ($field as $key => $value) {
				$fieldName   = array();
				$displayName = array();
				$fieldType   = array();
				if(str_contains($value, '<field=')){
					if(strlen(strrchr($value, '"')) == 1){
						$fieldName[$key] = substr(stristr($value, '"'), 1, strlen(stristr($value, '"'))-2);
					}
				}
				if(str_contains($value, 'displayName="')){
					if(strlen(strrchr($value, '"')) == 1){
						$displayName[$key] = substr(stristr($value, '"'), 1, strlen(stristr($value, '"'))-2);
					}
				}
				if(str_contains($value, 'filedType="')){
					if(strlen(strrchr($value, '"')) == 1){
						$fieldType[$key] = substr(stristr($value, '"'), 1, strlen(stristr($value, '"'))-2);
					}
				}
			}
			// $field = new Field;
			// $field->model_id     = $modelId;
			// $field->field_name   = $fieldName;
			// $field->display_name = $displayName;
			// $field->field_type   = $fieldType;
			// $field->save();
		}
	// return View::make('')->with
	}
	$str = strlen(strrchr('"12323"', '"'));
	dd($str);
});

Route::post('write', function(){
	//导出模型文件
	$modelId = Input::get('modelId');
	$model   = Model::find($modelId);
	$fields  = Field::where('model_id', $modelId)->get();
	$str     = '<meta content="text/html; charset=utf8" />'."\n";
	$str     = $str.'<model="'.$model['table_name'].'" />'."\n";
	foreach ($fields as $key => $field) {
		$str = $str.'<field="'.$field['field_name'].'" displayName="'.$field['display_name'].'" filedType="'.$field['field_type'].'" />'."\n";
	}
	return File::put("models/".$model['table_name'].".html", $str, true);
});

Route::get('test', 'Test@test');